package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.Collect;
import com.solo.eggsolo.entity.Singer;
import com.solo.eggsolo.service.CollectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author Yu
 *
 */
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏管理")
public class CollectController {
	@Autowired
	private CollectService collectService;

	@ApiOperation("获取收藏表信息")
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public Pager<Collect> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {

		Pager<Collect> pager = new Pager<>(page, size);

		List<Collect> list = collectService.list(pager);
		int total = collectService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}

	@ApiOperation("根据收藏表id查询信息")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET })
	public JsonResult findById(Integer id) {

		JsonResult result = new JsonResult(500, Boolean.FALSE, "查询失败");
		Collect collect = collectService.findById(id);

		if (collect != null) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", collect);
		}

		return result;
	}

	@ApiOperation("根据收藏者id查询收藏信息")
	@RequestMapping(value = "/findByName", method = { RequestMethod.GET })
	public Pager<Collect> findByName(Integer value) {

		Pager<Collect> pager = new Pager<>(1, 100);
		List<Collect> list = collectService.findByName(value);
		int total = collectService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}

	@ApiOperation("增肌收藏信息")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add(Collect collect) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "新增失败");

		boolean ret = collectService.add(collect);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "新增成功");
		}
		return result;
	}

	@ApiOperation("根据收藏表id编辑信息")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(Collect collect) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");
		boolean ret = collectService.edit(collect);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "修改成功");
		}
		return result;
	}

	@ApiOperation("根据收藏表id删除信息")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(Collect collect) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "删除失败");

		List<Collect> list = new ArrayList<Collect>();
		list.add(collect);

		boolean ret = collectService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "删除成功");
		}

		return result;
	}

}
